package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.dto.DishDto;
import com.example.entity.Category;
import com.example.entity.Dish;
import com.example.entity.DishFlavor;
import com.example.service.CategoryService;
import com.example.service.DishFlavorService;
import com.example.service.DishService;
import com.example.util.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜品管理 前端控制器
 * </p>
 *
 * @author bd
 * @since 2023-05-27
 */
@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private CategoryService categoryService;
    @GetMapping("/page")
    public R<Page> listDish(int page,int pageSize,String name)
    {
        Page<Dish> pageInfo=new Page<>(page,pageSize);
        LambdaQueryWrapper<Dish> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(!StringUtils.isEmpty(name),Dish::getName,name);
        lambdaQueryWrapper.orderByDesc(Dish::getCreateTime);
        dishService.page(pageInfo,lambdaQueryWrapper);
        //DTO
        Page<DishDto> pageDishDto=new Page<>(page,pageSize);
        pageDishDto=dishService.ListDishDto(pageInfo,pageDishDto);
        return R.success(pageDishDto);

    }
    //批量起售
    @PostMapping("/status/{statusId}")
    public R<String> changeStatus(@PathVariable int statusId,Long[] ids)
    {
        List<Dish> dishs=new ArrayList<>();
       for(Long id:ids)
       {
           Dish dish = dishService.getById(id);
           dish.setStatus(statusId);
           dishs.add(dish);
       }
        boolean b = dishService.updateBatchById(dishs);
        if(b)
        return R.success("停售成功");
        else
            return R.error("停售失败");
    }

    @DeleteMapping
    public R<String> deleteDish(Long[] ids)
    {
        System.out.println(ids);
        List<Long> idList=new ArrayList<>();
        for(Long id:ids)
        {
            idList.add(id);
        }
        boolean b = dishService.removeByIds(idList);
        if (b)
        return R.success("删除成功");
        else
       return R.error("删除失败");

    }

    //添加菜品
    @PostMapping
    public R<String> addNewDish(@RequestBody DishDto  dishdto)
    {
        //插入两张表
        boolean b = dishService.addnewDish(dishdto);
        if(b)
        return R.success("新增菜品成功");
        else
            return R.error("新增菜品失败");
    }

    //修改数据回显
    @GetMapping("/{ids}")
    public R<DishDto> changeDish(@PathVariable Long ids)
    {
        Dish dish = dishService.getById(ids);
        DishDto dishDto=new DishDto();
        BeanUtils.copyProperties(dish,dishDto);
        //查口味表
        LambdaQueryWrapper<DishFlavor>  lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DishFlavor::getDishId,ids);
        List<DishFlavor> list = dishFlavorService.list(lambdaQueryWrapper);

        dishDto.setFlavors(list);
        //查种类表
        Category category = categoryService.getById(dish.getCategoryId());
        dishDto.setCategoryName(category.getName());

        return R.success(dishDto);
    }

    //真修改
    @PutMapping
    @Transactional
    public R<String> update(@RequestBody DishDto dishDto)
    {
         dishService.updateById(dishDto);
        // dishFlavorService.saveOrUpdateBatch(dishDto.getFlavors());//这样有一个bug，在修改时如果新增一条口味，就找不到此条数据的dish_id;
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
       // List<DishFlavor> list = dishFlavorService.list(lambdaQueryWrapper);
        //遍历  把以前的口味数据全部删除，然后全部重新执行添加方法
        boolean remove = dishFlavorService.remove(lambdaQueryWrapper);
if(remove)
{
    //dishFlavorService.saveBatch(dishDto.getFlavors());//不妥，这样缺少Dish_id;所以要为每条数据附上dish_id
    List<DishFlavor> flavors = dishDto.getFlavors();
    for(DishFlavor dishFlavor:flavors)
    {
        dishFlavor.setDishId(dishDto.getId());
    }
    dishFlavorService.saveBatch(flavors);
}
        return R.success("修改成功！");
    }


    //新增(套餐分类下拉框)
    @GetMapping("/list")
    public R<List<Dish>> list(Long categoryId,String name)
    {
        LambdaQueryWrapper<Dish> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(categoryId!=null,Dish::getCategoryId,categoryId);
        lambdaQueryWrapper.eq(name!=null, Dish::getName,name);
        List<Dish> list = dishService.list(lambdaQueryWrapper);
        return R.success(list);
    }
}
